package MannageService;
import JDBC.DBHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManageDaoImp implements ManageService{
    @Override
    public Manage finduser(String managename) {
        Connection conn= DBHelper.getConn();
        //  String sql="select * from User WHERE id='"+id+"';";
        String sql="select * from manage WHERE mname=?;";
        PreparedStatement stat=null;
        ResultSet rs=null;
       Manage m=new Manage();
        try {
            stat=conn.prepareStatement(sql);
            stat.setString(1,managename);
            rs=stat.executeQuery();//executeQuery该方法用来执行查询语句
            while(rs.next()){

                m.setMname(rs.getString("mname"));
                m.setPassword(rs.getString("password"));
                m.setMphone(rs.getString("mphone"));
                m.setStorename(rs.getString("storename"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //       PreparedStatement stat = null;
            DBHelper.closeAll(conn,stat,rs);
        }
        return m;
    }

    @Override
    public void createNewUser(String storename, String mname, String password, String mphone) {
        Connection conn=DBHelper.getConn();
        String sql="INSERT manage(storename,mname,password,mphone) VALUE (?,?,?,?);";
        PreparedStatement stat=null;
        ResultSet rs=null;
        try{

            stat=conn.prepareStatement(sql);
            stat.setString(1, storename);
            stat.setString(2, mname);
            stat.setString(3, password);
            stat.setString(4, mphone);

            stat.executeUpdate();
            System.out.println("创建成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBHelper.closeAll(conn,stat,rs);
        }
    }

    @Override
    public void updateUser(Manage manage, String newpassword, String newphone, String newstorename) {
        Connection conn = DBHelper.getConn();
// String sql = "UPDATE User SET name='" + user.getName()+"',password='"+user.getPassword()+"', balance = "+user.getBalance()+" WHERE id='"+user.getID()+"';";
        String sql = "UPDATE manage SET storename=?,password=?,mphone =?WHERE mname=?";
        PreparedStatement stat = null;
        ResultSet rs = null;
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1, newstorename);
            stat.setString(2, newpassword);
            stat.setString(3, newphone);
            stat.setString(4, manage.getMname());
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, stat, rs);
        }
}
}
